package cn.jiahua.test5;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

    public static void main(String[] args) {
        Data data=new Data();

        new Thread(()->{
            for (int i=0;i<10;i++){
                data.printA();
            }
        },"A").start();

        new Thread(()->{
            for (int i=0;i<10;i++){
                data.printB();
            }
        },"B").start();


        new Thread(()->{
            for (int i=0;i<10;i++){
                data.printC();
            }
        },"C").start();





    }
}

//资源类
class Data{
    private int number=1;  //1.A 执行，2代表 B 3代表c
    Lock lock =new ReentrantLock();
    Condition conditionA =lock.newCondition();  //没满
    Condition conditionB=lock.newCondition(); //没空
    Condition conditionC=lock.newCondition(); //没空



    //a
    public void printA(){
        lock.lock();
        try{
        while(number!=1){
            conditionA.await();
        }
            System.out.println("AAAAAAAAAAAAA");
            number=2;
            conditionB.signal();
        }catch (Exception e){

        }finally {
            lock.unlock();
        }


    }


    public void  printB(){
        lock.lock();
        try{
            while(number!=2){
                conditionB.await();
            }
            System.out.println("BBBBBBBBBBBBB");
            number=3;
            conditionC.signal();
        }catch (Exception e){

        }finally {
            lock.unlock();
        }

    }


    public void printC(){
        lock.lock();
        try{
            while(number!=3){
                conditionC.await();
            }
            System.out.println("ccccccccccccccc");
            number=1;
            conditionA.signal();
        }catch (Exception e){

        }finally {
            lock.unlock();
        }

    }


}